<?php

namespace app\api\controller;

class Rent
{
    public $pagesize = 10;

    public function search()
    {
        $access_token = trim($_REQUEST['access_token']);
        $device_sn = trim($_REQUEST['device_sn']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token, 1, $_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        //查找是否是租赁设备
        $model = new \app\admin\model\RentDevice();
        $deviceInfo = $model->getDataForWhere(['device_sn'=>$device_sn,'is_delete'=>0]);
        $returnData=array();

        $returnData['is_rent_device'] =0;//是否是租用设备 0不是  1是
        $returnData['order_status'] =0;//无订单  1有订单已支付
        if(!empty($deviceInfo)){
            $returnData['is_rent_device'] =1;
        }

        //是否已经下单
        $payModel = new \app\admin\model\PayOrder();
        $OrderInfo = $payModel->getDataForWhere(['device_sn'=>$device_sn,'uid'=>$userInfo['id'],'is_delete'=>0],"*","id desc");
        if($OrderInfo['stat']==1){

        }
            $return['err'] = 0;
            $return['msg'] = '成功';
            $return['data'] = $returnData;
            return json($return);

    }



    public function check_koulin()
    {
        $koulin = trim($_REQUEST['koulin']);

        if (empty($koulin)) {
            $return['err'] = 10009;
            $return['msg'] = '口令不能为空';
            echo json_encode($return, true);
            exit;
        }

        $model = new \app\admin\model\RentKoulin();
        $Info = $model->getDataForWhere(['content'=>$koulin,'is_delete'=>0]);
        if(empty($Info)){
            $return['err'] = 10009;
            $return['msg'] = '口令不正确';
            echo json_encode($return, true);
            exit;
        }
        $model->updateForInc(array('id'=>$Info['id']),'cnt');

        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = $Info;
        return json($return);

    }


}
